//
//  MeLivePushWaitView.m
//  MeLive
//
//  Created by liulei on 2017/5/10.
//  Copyright © 2017年 liulei. All rights reserved.
//

#import "MeLivePushWaitView.h"
#import "MeLiveFilterView.h"

@interface MeLivePushWaitView ()<MeLiveFilterViewDelegate,UIGestureRecognizerDelegate>
{
    UIButton                    *_selectShareBtn;
    NSInteger                   _selectShareIndex;
    UILabel                     *_beautyBeautyLab;
    UILabel                     *_whiteBeautyLab;
    UISlider                    *_sdBeauty;
    UISlider                    *_sdWhitening;
    UIButton                    *_whiteBtn;
    UITapGestureRecognizer      *_tap;
    MeLiveFilterView            *_filterView;
    BOOL                        _isNoCoverImage;
}
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) UIButton *beautyBtn;
@property (nonatomic, strong) UIButton *cameraBtn;
@property (nonatomic, strong) UIButton *startBtn;
@property (strong, nonatomic) UIImageView *coverImageView;
@property (strong, nonatomic) UIImageView *cameraImage;
@property (strong, nonatomic) UIButton *timeLineBtn;
@end

@implementation MeLivePushWaitView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        _isNoCoverImage = YES;
        _tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickScreen:)];
        _tap.delegate = self;
        [self addGestureRecognizer:_tap];
        
        _selectShareIndex = 0;
        
        _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_closeBtn];
        [_closeBtn setImage:[UIImage imageNamed:@"live_back_new_icon.png"] forState:UIControlStateNormal];
        [_closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-15);
            make.top.equalTo(self.mas_top).offset(30);
            make.width.height.equalTo(@35);
        }];
        [_closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _beautyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_beautyBtn];
        [_beautyBtn setImage:[UIImage imageNamed:@"live_beauty_new_icon.png"] forState:UIControlStateNormal];
        [_beautyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(15);
            make.top.equalTo(self.mas_top).offset(30);
            make.width.height.equalTo(@35);
        }];
        [_beautyBtn addTarget:self action:@selector(beautyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _cameraBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:_cameraBtn];
        [_cameraBtn setImage:[UIImage imageNamed:@"live_camera_new_icon.png"] forState:UIControlStateNormal];
        [_cameraBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_beautyBtn.mas_right).offset(10);
            make.top.equalTo(self.mas_top).offset(30);
            make.width.height.equalTo(@35);
        }];
        [_cameraBtn addTarget:self action:@selector(cameraBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = [UIColor clearColor];
        [self addSubview:_bottomView];
        
        // 封面
        _coverImageView = [[UIImageView alloc] init];
        [self addSubview:_coverImageView];
        [_coverImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(15);
            make.top.equalTo(_closeBtn.mas_bottom).offset(20);
            make.width.height.equalTo(@160);
        }];
        [_coverImageView drawCornerRadius:5];
        
        _cameraImage = [[UIImageView alloc] init];
        _cameraImage.image = [UIImage imageNamed:@"live_publish_addcover_camera_icon.png"];
        [self addSubview:_cameraImage];
        [_cameraImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).offset(30);
            make.top.equalTo(_closeBtn.mas_bottom).offset(35);
            make.width.equalTo(@25);
            make.height.equalTo(@20);
        }];
        
        // 添加封面按钮
        UIButton *addCoverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:addCoverBtn];
        [addCoverBtn addTarget:self action:@selector(addCoverBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [addCoverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_coverImageView.mas_left);
            make.top.equalTo(_coverImageView.mas_top);
            make.bottom.equalTo(_coverImageView.mas_bottom);
            make.right.equalTo(_coverImageView.mas_right);
        }];
        
        [_bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self).offset(0);
            make.height.equalTo(@150);
        }];
        
        _startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_bottomView addSubview:_startBtn];
        [_startBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_startBtn setTitle:@"开启直播" forState:UIControlStateNormal];
        _startBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
        _startBtn.backgroundColor = UIColorFrom16RGB(0xff7701);
        [_startBtn drawCornerRadius:5];
        [_startBtn addTarget:self action:@selector(startBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_startBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_bottomView.mas_left).offset(45);
            make.right.equalTo(_bottomView.mas_right).offset(-45);
            make.bottom.equalTo(_bottomView.mas_bottom).offset(-60);
            make.height.equalTo(@40);
        }];
        
//        UILabel *tipsLabel = [[UILabel alloc] init];
//        [_bottomView addSubview:tipsLabel];
//        tipsLabel.text = @"邀请好友看我直播";
//        tipsLabel.textColor = [UIColor whiteColor];
//        tipsLabel.font = [UIFont fontWithName:@"Helvetica" size:14];
//        tipsLabel.textAlignment = NSTextAlignmentCenter;
//        
//        [tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(_bottomView.mas_left).offset(30);
//            make.right.equalTo(_bottomView.mas_right).offset(-30);
//            make.bottom.equalTo(_startBtn.mas_top).offset(-60);
//            make.height.equalTo(@20);
//        }];
//        
//        UIButton *qqBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_bottomView addSubview:qqBtn];
//        qqBtn.tag = 102;
//        [qqBtn setImage:[UIImage imageNamed:@"live_publish_qq_new_icon.png"] forState:UIControlStateNormal];
//        [qqBtn setImage:[UIImage imageNamed:@"live_publish_qq_select_new_icon.png"] forState:UIControlStateSelected];
//        [qqBtn addTarget:self action:@selector(selectShareTypeClick:) forControlEvents:UIControlEventTouchUpInside];
//        [qqBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.equalTo(_bottomView.mas_centerX);
//            make.bottom.equalTo(_startBtn.mas_top).offset(-25);
//            make.width.height.equalTo(@30);
//        }];
//        
//        UIButton *wechatBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_bottomView addSubview:wechatBtn];
//        wechatBtn.tag = 101;
//        [wechatBtn setImage:[UIImage imageNamed:@"live_publish_wechat_new_icon.png"] forState:UIControlStateNormal];
//        [wechatBtn setImage:[UIImage imageNamed:@"live_publish_wechat_select_new_icon.png"] forState:UIControlStateSelected];
//        [wechatBtn addTarget:self action:@selector(selectShareTypeClick:) forControlEvents:UIControlEventTouchUpInside];
//        [wechatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(qqBtn.mas_centerY);
//            make.right.equalTo(qqBtn.mas_left).offset(-25);
//            make.width.height.equalTo(@30);
//        }];
//        
//        _timeLineBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_bottomView addSubview:_timeLineBtn];
//        [self selectShareTypeClick:_timeLineBtn];
//        _timeLineBtn.tag = 100;
//        [_timeLineBtn setImage:[UIImage imageNamed:@"live_publish_timeline_new_icon.png"] forState:UIControlStateNormal];
//        [_timeLineBtn setImage:[UIImage imageNamed:@"live_publish_timeline_select_new_icon.png"] forState:UIControlStateSelected];
//        [_timeLineBtn addTarget:self action:@selector(selectShareTypeClick:) forControlEvents:UIControlEventTouchUpInside];
//        [_timeLineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(wechatBtn.mas_centerY);
//            make.right.equalTo(wechatBtn.mas_left).offset(-25);
//            make.width.height.equalTo(@30);
//        }];
//        UIButton *qqspaceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_bottomView addSubview:qqspaceBtn];
//        qqspaceBtn.tag = 103;
//        [qqspaceBtn setImage:[UIImage imageNamed:@"live_publish_qqspace_new_icon.png"] forState:UIControlStateNormal];
//        [qqspaceBtn setImage:[UIImage imageNamed:@"live_publish_qqspace_select_new_icon.png"] forState:UIControlStateSelected];
//        [qqspaceBtn addTarget:self action:@selector(selectShareTypeClick:) forControlEvents:UIControlEventTouchUpInside];
//        [qqspaceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(qqBtn.mas_centerY);
//            make.left.equalTo(qqBtn.mas_right).offset(25);
//            make.width.height.equalTo(@30);
//        }];
//        UIButton *weiboBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [_bottomView addSubview:weiboBtn];
//        weiboBtn.tag = 104;
//        [weiboBtn setImage:[UIImage imageNamed:@"live_publish_weibo_new_icon.png"] forState:UIControlStateNormal];
//        [weiboBtn setImage:[UIImage imageNamed:@"live_publish_weibo_select_new_icon.png"] forState:UIControlStateSelected];
//        [weiboBtn addTarget:self action:@selector(selectShareTypeClick:) forControlEvents:UIControlEventTouchUpInside];
//        [weiboBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(qqspaceBtn.mas_centerY);
//            make.left.equalTo(qqspaceBtn.mas_right).offset(25);
//            make.width.height.equalTo(@30);
//        }];
//        
        //美颜拉杆浮层
        _vBeauty = [[UIView  alloc] init];
        [_vBeauty setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
        _vBeauty.hidden = YES;
        
        [self addSubview: _vBeauty];
        
        UILabel *beautyTitle = [[UILabel alloc] init];
        [_vBeauty addSubview:beautyTitle];
        beautyTitle.textAlignment = NSTextAlignmentCenter;
        beautyTitle.textColor = [UIColor whiteColor];
        beautyTitle.text = @"开启美颜";
        
        UIView *beautyLineView = [[UIView alloc] init];
        [_vBeauty addSubview:beautyLineView];
        beautyLineView.backgroundColor = UIColorFrom16RGB(0xcccccc);
        
        _whiteBeautyLab = [[UILabel alloc] init];
        _whiteBeautyLab.text = @"美白";
        _whiteBeautyLab.font = [UIFont systemFontOfSize:14];
        _whiteBeautyLab.textColor = [UIColor whiteColor];
        [_vBeauty addSubview:_whiteBeautyLab];
        
        _beautyBeautyLab = [[UILabel alloc] init];
        _beautyBeautyLab.text = @"美颜";
        _beautyBeautyLab.font = [UIFont systemFontOfSize:14];
        _beautyBeautyLab.textColor = [UIColor whiteColor];
        [_vBeauty addSubview:_beautyBeautyLab];
        
        
        _sdWhitening = [[UISlider alloc] init];
        _sdWhitening.minimumValue = 0;
        _sdWhitening.maximumValue = 9;
        [_sdWhitening addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
        _sdWhitening.tag = 1;
        _sdWhitening.minimumTrackTintColor = UIColorFrom16RGB(0xfa7221);
        _sdWhitening.maximumTrackTintColor = [UIColor whiteColor];
        _sdWhitening.value = 3;
        [_vBeauty addSubview:_sdWhitening];
        
        _sdBeauty = [[UISlider alloc] init];
        _sdBeauty.minimumValue = 0;
        _sdBeauty.maximumValue = 9;
        _sdBeauty.minimumTrackTintColor = UIColorFrom16RGB(0xfa7221);
        _sdBeauty.maximumTrackTintColor = [UIColor whiteColor];
        _sdBeauty.value = 7;
        [_sdBeauty addTarget:self action:@selector(sliderValueChange:) forControlEvents:UIControlEventValueChanged];
        _sdBeauty.tag = 0;
        [_vBeauty addSubview:_sdBeauty];
        
        _filterView = [[MeLiveFilterView alloc] initWithBeautyViewW:MeLiveScreenWidth];
        _filterView.delegate = self;
        [_vBeauty addSubview:_filterView];
        
        [_vBeauty mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self).offset(0);
            make.height.equalTo(@240);
        }];
        
        [beautyTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_vBeauty.mas_centerX).offset(0);
            make.top.equalTo(_vBeauty.mas_top).offset(15);
            make.width.equalTo(@100);
            make.height.equalTo(@20);
        }];
        
        [beautyLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(beautyTitle.mas_bottom).offset(15);
            make.left.equalTo(_vBeauty.mas_left).offset(10);
            make.right.equalTo(_vBeauty.mas_right).offset(-10);
            make.height.equalTo(@1);
        }];
        
        [_whiteBeautyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(beautyLineView.mas_bottom).offset(20);
            make.left.equalTo(_vBeauty.mas_left).offset(20);
            make.height.width.equalTo(@30);
        }];
        
        [_sdWhitening mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_whiteBeautyLab.mas_right).offset(10);
            make.centerY.equalTo(_whiteBeautyLab.mas_centerY);
            make.right.equalTo(_vBeauty.mas_right).offset(-20);
            make.height.equalTo(@20);
        }];
        
        [_beautyBeautyLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_whiteBeautyLab.mas_bottom).offset(20);
            make.left.equalTo(_vBeauty.mas_left).offset(20);
            make.height.width.equalTo(@30);
        }];
        
        [_sdBeauty mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_beautyBeautyLab.mas_right).offset(10);
            make.centerY.equalTo(_beautyBeautyLab.mas_centerY);
            make.right.equalTo(_vBeauty.mas_right).offset(-20);
            make.height.equalTo(@20);
        }];
        
        [_filterView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_beautyBeautyLab.mas_bottom).offset(20);
            make.left.equalTo(_vBeauty.mas_left).offset(0);
            make.right.equalTo(_vBeauty.mas_right).offset(0);
            make.height.equalTo(@70);
        }];
    }
    return self;
}

-(void)sliderValueChange:(UISlider*) slider{
    if ([self.delegate respondsToSelector:@selector(didClickBeautysliderValueChange:beautySlider:)]) {
        [self.delegate didClickBeautysliderValueChange:self beautySlider:slider];
    }
}

- (void)closeBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(didClickCloseBtnInWaitPushView:)]) {
        [self.delegate didClickCloseBtnInWaitPushView:self];
    }
}

- (void)beautyBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(didClickBeautyBtnInWaitPushView:)]) {
        [self.delegate didClickBeautyBtnInWaitPushView:self];
    }
}

- (void)cameraBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(didClickCameraBtnInWaitPushView:)]) {
        [self.delegate didClickCameraBtnInWaitPushView:self];
    }
}

- (void)setCoverPicStr:(NSString *)coverPicStr{
    if ([coverPicStr isEqualToString:@""]) {
        _isNoCoverImage = YES;
        _coverImageView.image = [UIImage imageNamed:@"live_publish_zan_wei_icon.png"];
    }else{
        _isNoCoverImage = NO;
        [_coverImageView sd_setImageWithURL:[NSURL URLWithString:coverPicStr] placeholderImage:[UIImage imageNamed:@"live_publish_loading_icon.png"]];
    }
}

- (void)setIsHaveCover:(BOOL)isHaveCover{
    _isHaveCover = isHaveCover;
    _isNoCoverImage = NO;
}

- (void)addCoverBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(didClickAddCoverPicInWaitPushView:coverImage:)]) {
        [self.delegate didClickAddCoverPicInWaitPushView:self coverImage:_coverImageView];
    }
}

- (void)startBtnClick:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(didClickStartBtnInWaitPushView:selectShareIndex:isNoCoverImage:)]) {
        [self.delegate didClickStartBtnInWaitPushView:self selectShareIndex:_selectShareIndex isNoCoverImage:_isNoCoverImage];
    }
}

- (void)selectShareTypeClick:(UIButton *)btn{
    
    if (btn != _selectShareBtn) {
        _selectShareBtn.selected = NO;
    }
    
    btn.selected =! btn.selected;
    _selectShareBtn = btn;
    if (btn.selected) {
        switch (btn.tag) {
            case 100:
                _selectShareIndex = 0;
                break;
            case 101:
                _selectShareIndex = 1;
                break;
            case 102:
                _selectShareIndex = 2;
                break;
            case 103:
                _selectShareIndex = 3;
                break;
            case 104:
                _selectShareIndex = 4;
                break;
                
            default:
                break;
        }
    }else{
        _selectShareIndex = -1;
    }
}

-(void)clickScreen:(UITapGestureRecognizer *)gestureRecognizer{
    if ([self.delegate respondsToSelector:@selector(didClickScreenInWaitPushView:gestureRecognizer:)]) {
        [self.delegate didClickScreenInWaitPushView:self gestureRecognizer:gestureRecognizer];
    }
}

#pragma mark - XLJLiveFilterViewDelegate
- (void)didSelectFilterInXLJLiveFileterView:(MeLiveFilterView *)filterView selectIndex:(NSInteger)selectIndex{
    if ([self.delegate respondsToSelector:@selector(didSelectFilterInWaitPushView:selectFilterIndex:)]) {
        [self.delegate didSelectFilterInWaitPushView:self selectFilterIndex:selectIndex];
    }
}

#pragma mark - 过滤giftView上的手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([touch.view isDescendantOfView:_vBeauty]) {
        return NO;
    }
    return YES;
}

@end
